package m.jclecai.com.pay;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import m.jclecai.com.activity.WebHelperActivity;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.PayChannelBean;


/**
 * Created by Dinosa on 2017/12/29.
 * 充值的一个默认的实现的及基类；
 */

public abstract class BasePayImpl implements IPay {

    public BaseActivity mActivity;

    public String mOrderNumber;

    public boolean isPaying=false;

    private Map<String, String> urls = new HashMap<String, String>() {
        {
//            put("sumpay", "http://m.jclecai.com/user/sumpay.html");
            put("alipayh5", "http://j5.jclecai.com/user/alipayh5.html");
            put("sumpayh5", "http://j5.jclecai.com/user/sumpay.html");
            put("scpayh5", "https://m.yuecp.net/user/redirect.html");
            put("ysepayh5", "http://j5.jclecai.com/user/ysepay.html");
            put("yspayh5", "http://j5.jclecai.com/user/ysepay.html");
            put("iapppay", "http://j5.jclecai.com/user/iapppay.html");

            //            put("sumpay", "https://m.yuecp.net/user/sumpay.html");
//            put("alipayh5", "https://m.yuecp.net/user/alipayh5.html");
//            put("sumpayh5", "https://m.yuecp.net/user/sumpay.html");
//            put("scpayh5", "https://m.yuecp.net/user/redirect.html");
//            put("ysepayh5", "https://m.yuecp.net/user/ysepay.html");
//            put("yspayh5", "https://m.yuecp.net/user/ysepay.html");
//            put("iapppay", "https://m.yuecp.net/user/iapppay.html");
        }
    };

    public BasePayImpl(BaseActivity activity) {
        mActivity = activity;
    }

    public void showDialog(){
        mActivity.showDialog();
    }

    public void dissDialog(){
        mActivity.dissDialog();
    }

    public void DisPlay(String content){
        mActivity.DisPlay(content);
    }

    public String getString(int resId){
        return mActivity.getString(resId);
    }

    public void startActivity(Intent intent){
        mActivity.startActivity(intent);

    }

    public void startActivityForResult(Intent intent, int requestCode, Bundle options){
        mActivity.startActivityForResult(intent,requestCode,options);
    }

    public void startActivityForResult(Intent intent,int requestCode){
        mActivity.startActivityForResult(intent,requestCode);
    }

    /**
     * 设置订单号；
     * @param orderNumber
     */
    public void setOrderNumber(String orderNumber) {
        mOrderNumber = orderNumber;
    }

    @Override
    public String getOrderNumber() {
        return mOrderNumber;
    }

    /**
     * 设置支付的状态；
     * @param isPaying
     */
    public void setPaying(boolean isPaying){
        this.isPaying=isPaying;
    }

    @Override
    public boolean isPaying() {
        return isPaying;
    }

    /**
     * 清空订单号；
     */
    public void clearOrderNumber(){
        mOrderNumber="";
    }

    //跳转app内部webview
    protected void weakUpAPPPay(String key, String param, String name) {
        dissDialog();
        //使用浏览器打包；
        startActivity(WebHelperActivity.getInstance(mActivity,
                urls.get(key) + parserURLParams(param),
                false,
                name,
                true));
        //这里是设置支付状态；
        setPaying(true);
    }

    public void weakUpPay(String url,boolean paying){
        dissDialog();
        //使用浏览器打包；
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(url);
        intent.setData(content_url);
        startActivity(intent);
        //这里是设置支付状态；
        setPaying(paying);
    }

    public void weakUpPay(String url) {
        weakUpPay(url,true);
    }

    protected void weakUpPay(String key, String param) {
        dissDialog();
        //使用浏览器打包；
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(urls.get(key) + parserURLParams(param));
        intent.setData(content_url);
//        Log.e("weakUpPay",content_url.toString());
        //intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
        startActivity(intent);
        //这里是设置支付状态；
        setPaying(true);
    }

    protected String parserURLParams(String param2) {
        Map<String, String> map = new Gson().fromJson(param2, new TypeToken<Map<String, String>>() {
        }.getType());
        StringBuffer param = new StringBuffer("?");
        for (Map.Entry<String, String> next : map.entrySet()) {
            param.append(next.getKey());
            param.append("=");
            param.append(next.getValue());
            param.append("&");
        }
        param = param.deleteCharAt(param.length() - 1);
        Log.e("param", param.toString());
        return param.toString();
    }

    public abstract void pay(PayChannelBean channelBean, String payMoney);
}
